#!/bin/bash

set -ex
CURRENT_DIR=$(cd "$(dirname $0)" && pwd)
BASE_PATH=$(readlink -f "${CURRENT_DIR}/../../../")
OPENSOURCE_REPOSITORY_DIR=${binary_path}

FINAL_PKG_PATH=${BASE_PATH}/final_pkg

WORK_HOME=${BASE_PATH}

MakeDir()
{
    if [ ! -d "$1" ]; then
        mkdir -p "$1"
        echo "mkdir $1"
    fi
}

CpFile()
{
    if [ ! -f "$1" ]; then
        echo "=== $1 file not exist ==="
        return 1
    fi
    cp "$1" "$2"
    echo "copy file $1 to $2"
}

CheckTools()
{
	which unix2dos
	if [ $? -ne 0 ]; then
		echo "there is no tool unix2dos for adapting bat script."
		return 1
	fi
}

PrepareDir()
{
	cd ${BASE_PATH}/
	MakeDir temp
	MakeDir Plugins
	MakeDir tmp_zip

	if [ -d "final_pkg" ]; then
		rm -rf final_pkg/*
	else
		MakeDir final_pkg
	fi
	MakeDir final_pkg/ProtectClient-e
	MakeDir final_pkg/third_party_software
	MakeDir final_pkg/PackageScript
	MakeDir final_pkg/Plugins
	MakeDir final_pkg/package
	MakeDir final_pkg/PackageScript/windows
	MakeDir final_pkg/PackageScript/like-unix
}

Clean()
{
	cd ${BASE_PATH}/
	rm -rf tmp_zip
	rm -rf Plugins
	rm -rf temp
}

PreparePackageRule()
{
	cp -rf Agent/ci/PackingRules/* ${FINAL_PKG_PATH}/package
}

PreparePlugins()
{
	# Plugins
	CpFile ${OPENSOURCE_REPOSITORY_DIR}/Plugins/Linux/x86_64/HadoopPlugin.tar.gz ${FINAL_PKG_PATH}/Plugins
	CpFile ${OPENSOURCE_REPOSITORY_DIR}/Plugins/Linux/x86_64/ElasticSearchPlugin.tar.gz ${FINAL_PKG_PATH}/Plugins

	CpFile ${OPENSOURCE_REPOSITORY_DIR}/Plugins/Linux/NasPlugin.tar.xz ${FINAL_PKG_PATH}/Plugins
	CpFile ${OPENSOURCE_REPOSITORY_DIR}/Plugins/Linux/FilePlugin.tar.xz ${FINAL_PKG_PATH}/Plugins
	CpFile ${OPENSOURCE_REPOSITORY_DIR}/Plugins/Linux/GeneralDBPlugin.tar.xz ${FINAL_PKG_PATH}/Plugins
	CpFile ${OPENSOURCE_REPOSITORY_DIR}/Plugins/Linux/Block_Service.tar.xz ${FINAL_PKG_PATH}/Plugins
	CpFile ${OPENSOURCE_REPOSITORY_DIR}/Plugins/Linux/VirtualizationPlugin.tar.xz ${FINAL_PKG_PATH}/Plugins
	CpFile ${OPENSOURCE_REPOSITORY_DIR}/Plugins/Linux/FusionComputePlugin.tar.xz ${FINAL_PKG_PATH}/Plugins
	CpFile ${OPENSOURCE_REPOSITORY_DIR}/Plugins/Linux/ObsPlugin.tar.xz ${FINAL_PKG_PATH}/Plugins

	CpFile ${OPENSOURCE_REPOSITORY_DIR}/Plugins/Linux/aarch64/cppframework-Linux_aarch64.tar.xz ${FINAL_PKG_PATH}/Plugins
	CpFile ${OPENSOURCE_REPOSITORY_DIR}/Plugins/Linux/x86_64/cppframework-Linux_x86_64.tar.xz ${FINAL_PKG_PATH}/Plugins

	CpFile ${OPENSOURCE_REPOSITORY_DIR}/Plugins/Windows/FilePlugin.zip ${FINAL_PKG_PATH}/Plugins
	CpFile ${OPENSOURCE_REPOSITORY_DIR}/Plugins/Windows/VirtualizationPlugin.zip ${FINAL_PKG_PATH}/Plugins
	CpFile ${OPENSOURCE_REPOSITORY_DIR}/Plugins/Windows/GeneralDBPlugin.zip ${FINAL_PKG_PATH}/Plugins

	CpFile ${OPENSOURCE_REPOSITORY_DIR}/Plugins/AIX/FilePlugin_ppc_64.tar.xz ${FINAL_PKG_PATH}/Plugins
	CpFile ${OPENSOURCE_REPOSITORY_DIR}/Plugins/AIX/GeneralDBPlugin_ppc_64.tar.xz ${FINAL_PKG_PATH}/Plugins
	CpFile ${OPENSOURCE_REPOSITORY_DIR}/Plugins/AIX/cppframework-AIX_ppc_64.tar.xz ${FINAL_PKG_PATH}/Plugins

	CpFile ${OPENSOURCE_REPOSITORY_DIR}/Plugins/Solaris/FilePlugin_sun4v.tar.gz ${FINAL_PKG_PATH}/Plugins
	CpFile ${OPENSOURCE_REPOSITORY_DIR}/Plugins/Solaris/cppframework-SunOS_sun4v.tar.gz ${FINAL_PKG_PATH}/Plugins
}

PrepareDeployScript()
{
	# PackageScript
	cd tmp_zip
	find ${BASE_PATH}/Agent/bin/install/ -maxdepth 1 \( -name "*sh" -o -name "*txt*" \)  -exec cp -f "{}" "." \;
	zip -r ${FINAL_PKG_PATH}/PackageScript/package-like-unix.zip .
	rm -rf ${BASE_PATH}/tmp_zip/*

	find ${BASE_PATH}/Agent/bin/install/ -maxdepth 1 \( -name "*bat" -o -name "*txt*" \)  -exec cp -f "{}" "." \;
	find ./ -name "*.bat" | xargs unix2dos
	zip -r ${FINAL_PKG_PATH}/PackageScript/package-windows.zip .
	rm -rf ${BASE_PATH}/tmp_zip/*

	cp ${BASE_PATH}/Agent/bin/bat/push_install_check.bat ${FINAL_PKG_PATH}/PackageScript/windows
	cp ${BASE_PATH}/Agent/bin/shell/push_install_check.sh ${FINAL_PKG_PATH}/PackageScript/like-unix
	cd ${BASE_PATH}/
}

PrepareThirdParty()
{
    # 准备 dataturbo
    if [ -d "${WORK_HOME}/dataturbo_pkg" ]; then
        rm -rf ${WORK_HOME}/dataturbo_pkg/*
    else
        mkdir ${WORK_HOME}/dataturbo_pkg
    fi

    cp ${OPENSOURCE_REPOSITORY_DIR}/MountTools/OceanStor_DataTurbo_*_Linux.zip ${WORK_HOME}/dataturbo_pkg/dataturbo.zip
	cp ${OPENSOURCE_REPOSITORY_DIR}/MountTools/OceanStor_DataTurbo_*_Linux.tar.gz ${WORK_HOME}/dataturbo_pkg/dataturbo.tar.gz
    cp ${OPENSOURCE_REPOSITORY_DIR}/MountTools/OceanStor_DataTurbo_*_Windows.zip ${WORK_HOME}/dataturbo_pkg/dataturbo-windows.zip

	# 准备 fileclient
    if [ -d "${WORK_HOME}/fileclient_pkg" ]; then
        rm -rf ${WORK_HOME}/fileclient_pkg/*
    else
        mkdir ${WORK_HOME}/fileclient_pkg
    fi
    cp ${OPENSOURCE_REPOSITORY_DIR}/MountTools/fileClient_aarch64.tar.gz ${WORK_HOME}/fileclient_pkg/
    cp ${OPENSOURCE_REPOSITORY_DIR}/MountTools/fileClient_x86_64.tar.gz ${WORK_HOME}/fileclient_pkg/

	cd tmp_zip

	# linux 3rd zip
	CpFile ${WORK_HOME}/dataturbo_pkg/dataturbo.zip .
	CpFile ${WORK_HOME}/dataturbo_pkg/dataturbo.tar.gz .
	CpFile ${WORK_HOME}/fileclient_pkg/fileClient_aarch64.tar.gz .
	CpFile ${WORK_HOME}/fileclient_pkg/fileClient_x86_64.tar.gz .
	zip -r ${FINAL_PKG_PATH}/third_party_software/3rd-linux.zip .
	rm -rf ${BASE_PATH}/tmp_zip/*
	
	# windows 3rd zip
	mkdir -p ${BASE_PATH}/Agent/open_src
	cp ${OPENSOURCE_REPOSITORY_DIR}/ThirdParty/Windows2012/x64/third_party_groupware/OceanProtect_X8000_1.2.1RC1_Opensrc_3rd_SDK_Windows2012_x64.zip ${BASE_PATH}/Agent/open_src/
	unzip ${BASE_PATH}/Agent/open_src/OceanProtect_X8000_1.2.1RC1_Opensrc_3rd_SDK_Windows2012_x64.zip "7zip/lib/7z.*" -d ./unzip
	mkdir ./7Zip
	cp -rf ${BASE_PATH}/tmp_zip/unzip/7zip/lib/7z.* ./7Zip
	rm -rf ./unzip

	CpFile ${WORK_HOME}/dataturbo_pkg/dataturbo-windows.zip .
	zip -r ${FINAL_PKG_PATH}/third_party_software/3rd-windows.zip .
	rm -rf ${BASE_PATH}/tmp_zip/*

	# AIX xz
	CpFile ${OPENSOURCE_REPOSITORY_DIR}/ThirdParty/Unix/AIX/opensrc_xz.zip ${FINAL_PKG_PATH}/third_party_software/opensrc_xz.zip

	cd ${BASE_PATH}/
	rm -rf ${WORK_HOME}/dataturbo_pkg
	rm -rf ${WORK_HOME}/fileclient_pkg
}

PrepareProtectClient()
{
	if [ ! -d ${WORK_HOME}/temp ]; then
		mkdir -p ${WORK_HOME}/temp
	fi
	cp -rf ${OPENSOURCE_REPOSITORY_DIR}/dependency/Linux/* ${BASE_PATH}/temp
	cp -rf ${OPENSOURCE_REPOSITORY_DIR}/dependency/common/*AIX*.xz ${BASE_PATH}/temp
	cp -rf ${OPENSOURCE_REPOSITORY_DIR}/dependency/common/*SunOS*.gz ${BASE_PATH}/temp
	cp -rf ${OPENSOURCE_REPOSITORY_DIR}/dependency/Windows/* ${BASE_PATH}/temp

    find ${BASE_PATH}/temp/ -maxdepth 1 -name "protectclient*Linux*.tar.xz"  -exec cp -f "{}" "${FINAL_PKG_PATH}/ProtectClient-e" \;
    find ${BASE_PATH}/temp/ -maxdepth 1 -name "protectclient*Linux*.tar.xz"  -exec cp -f "{}" "${FINAL_PKG_PATH}/ProtectClient-e" \;
    find ${BASE_PATH}/temp/ -maxdepth 1 -name "protectclient*.tar.gz"  -exec cp -f "{}" "${FINAL_PKG_PATH}/ProtectClient-e" \;
    find ${BASE_PATH}/temp/ -maxdepth 1 \( -name "protectclient*.tar.xz" -a ! -name "protectclient*Linux*.tar.xz" \) -exec cp -f "{}" "${FINAL_PKG_PATH}/ProtectClient-e" \;
    find ${BASE_PATH}/temp/ -maxdepth 1 -name "sanclient*.tar.xz" -exec cp -f "{}" "${FINAL_PKG_PATH}/ProtectClient-e" \;
    find ${BASE_PATH}/temp/ -maxdepth 1 -name "protectclient*Windows*.zip" -exec cp -f "{}" "${FINAL_PKG_PATH}/ProtectClient-e" \;

	rm -rf ${WORK_HOME}/temp
}

Pack()
{
	# 复制文件，打包到ZIP
	cp -rf ${BASE_PATH}/Agent/ci/script/package.json ${BASE_PATH}/

	# 修改package.json 版本号
	sh ${BASE_PATH}/Agent/ci/script/set_package_version.sh ${BASE_PATH}/package.json  ${MS_IMAGE_TAG} ${Version}

	#sha256 files
	mv ${BASE_PATH}/package.json ${FINAL_PKG_PATH}
	cd ${FINAL_PKG_PATH} && find -type f | xargs sha256sum >../sha256sum_sync
	mv ../sha256sum_sync .
	echo "agent files sha256 finish"

	cd ${FINAL_PKG_PATH}
	zip -r DataProtect_${Version}_client.zip ./*

	echo "agent pack finish"
}

CheckTools
if [ $? -ne 0 ]; then
	echo "Check tool fail."
	exit 1
fi

PrepareDir

PreparePackageRule

PreparePlugins

PrepareDeployScript

PrepareThirdParty

PrepareProtectClient

Pack

Clean
